package com.shoukaiseki.xutils.simple;


import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Test007Activity extends Activity{
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test007);
		
		final EditText message=(EditText) findViewById(R.id.test007_message);
		final EditText username=(EditText) findViewById(R.id.edit_username);
		final EditText password=(EditText) findViewById(R.id.edit_password);
		Button loginbutton=(Button) findViewById(R.id.loginbutton);
		
		//设置只读
		message.setFocusable(false);

		 loginbutton.setOnClickListener(new View.OnClickListener() {

			 @Override
			 public void onClick(View arg0) {
				 // TODO Auto-generated method stub
				 HttpUtils http = new HttpUtils();
				 // 设置缓存1ms，1ms内直接返回上次成功请求的结果。
				 http.configCurrentHttpCacheExpiry(1);
				 
				 RequestParams params = new RequestParams();
				 params.addQueryStringParameter("username", username.getText().toString());
				 params.addQueryStringParameter("password", password.getText().toString());
				 
				 http.send(HttpRequest.HttpMethod.GET,
						 "http://maximodecryption.duapp.com/android/login.jsp",
						 params,
						 new RequestCallBack<String>(){

					 @Override
					 public void onFailure(HttpException arg0, String msg) {
						 // TODO Auto-generated method stub
						 message.setText("onFailure:"+msg);
					 }

					 @Override
					 public void onSuccess(ResponseInfo<String> responseInfo) {
						 // TODO Auto-generated method stub
						 message.setText("upload response:" +responseInfo.result);
				            try {
				            	JSONTokener jsonParser=new JSONTokener(responseInfo.result);
				            	
				            	 // 此时还未读取任何json文本，直接读取就是一个JSONObject对象。  
				            	JSONObject json = (JSONObject) jsonParser.nextValue();  
				            	String status = json.getString("status");
				            	String username = json.getString("username");
				            	String password = json.getString("password");
				            	String datestring= json.getString("JSON");
				            	String osName= json.getString("osName");
				            	String osArch= json.getString("osArch");
				            	String osVersion= json.getString("osVersion");
				            	message.setText(status
				            			+"\n"+"username="+username
				            			+"\n"+"password="+password
				            			+"\n"+"时间="+datestring
				            			+"\n"+"服务器操作系统名称="+osName
				            			+"\n"+"服务器操作系统构架="+osArch
				            			+"\n"+"服务器操作系统版本="+osVersion
				            			);
							} catch (JSONException ex) {
								// TODO: handle exception
							}

					 }
				 });
			 }


		 });

	}	

}
